package com.yc.cloud.portal.service.impl;

import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.entity.Test;
import com.yc.cloud.mapper.TestMapper;
import com.yc.cloud.portal.service.TestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2025-07-07
 */
@Service
        public class TestServiceImpl extends ServiceImpl<TestMapper, Test>implements TestService {


        @Override
        public IPage<Test>list(BasePageReqDTO queryParam){
        val query=new QueryWrapper<Test>();
        IPage<Test>page;
        //排序
        if(StrUtil.isNotEmpty(queryParam.getSortBy())){
        if(BasePageReqDTO.ORDER_DESC.equals(queryParam.getOrder())){
        query.orderByDesc(queryParam.getSortBy());
        }else if(BasePageReqDTO.ORDER_ASC.equals(queryParam.getOrder())){
        query.orderByAsc(queryParam.getSortBy());
        }
        }
        //模糊查询
        if(StrUtil.isNotEmpty(queryParam.getKeyword())){
        query.and(queryWrapper->{

        });
        }
        //分页
        if(queryParam.getPage()){
        page=new Page<>(queryParam.getPageNum(),queryParam.getPageSize(),true);
        }else{
        page=new Page<>(queryParam.getPageNum(),baseMapper.selectCount(query),true);
        }
        return baseMapper.selectPage(page,query);
        }

        @Override
        public Test create(Test param){
        val test =new Test();
        BeanUtils.copyProperties(param, test);
        val saveFlag=save(test);
        return saveFlag? test :null;
        }

        @Override
        public Test get(Long id){
        return getById(id);
        }

        @Override
        public boolean delete(Long id){
        return removeById(id);
        }

        @Override
        public Test update(Long id,Test param){
        val test =new Test();
        BeanUtils.copyProperties(param,test);
    test.setId(id);
        val updateFlag=updateById(test);
        return updateFlag?test:null;
        }


        @Override
        public Test convertVo(Test test){
        if(test==null){
        return null;
        }
        val vo=new Test();
        BeanUtils.copyProperties(test,vo);
        return vo;
        }

        @Override
        public List<Test> convertVo(List<Test> TestList) {
        if (CollUtil.isNotEmpty(TestList)) {
        return TestList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
        }
        }
